// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import axios from 'axios'
import router from './router'
import store from './store'

Vue.config.productionTip = false

axios.defaults.timeout =  6000;
axios.defaults.retry = 1; //重试次数
axios.defaults.retryDelay = 1000;//重试延时
axios.defaults.shouldRetry = (error) => true;//重试条件，默认只要是错误都需要重试


axios.interceptors.request.use(function (config){   //所有的请求都会先走这里
  // console.log(config)
  // console.log('我拦截用户请求')
  return config
})

axios.interceptors.response.use(undefined, (err) => {
  var config = err.config;
  // 判断是否配置了重试
  if(!config || !config.retry) return Promise.reject(err);

  if(!config.shouldRetry || typeof config.shouldRetry != 'function') {
     return Promise.reject(err);
  }

  //判断是否满足重试条件
  if(!config.shouldRetry(err)) {
    return Promise.reject(err);
  }

  // 设置重置次数，默认为0
  config.__retryCount = config.__retryCount || 0;

  // 判断是否超过了重试次数
  if(config.__retryCount >= config.retry) {
      console.log(err);
      return Promise.reject(err);
  }

  //重试次数自增
  config.__retryCount += 1;

  //延时处理
  var backoff = new Promise(function(resolve) {
      setTimeout(function() {
          resolve();
      }, config.retryDelay || 1);
  });

  //重新发起axios请求
  return backoff.then(function() {
      return axios(config);
  });
});

axios.interceptors.request.use(function (config){   //所有的响应都会先走这里
  // console.log(config)
  // console.log('我拦截用户响应')
  return config
})


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})


